package com.acwer.drools.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.acwer.drools.dao.BenchMarkDao;
import com.acwer.drools.dao.CHSDRGDao;
import com.acwer.drools.dao.CcDao;
import com.acwer.drools.dao.ICD10YBDao;
import com.acwer.drools.dao.ICD9YBDao;
import com.acwer.drools.dao.JBBMDao;
import com.acwer.drools.dao.Jbbm20Dao;
import com.acwer.drools.dao.MDCDao;
import com.acwer.drools.dao.MccDao;
import com.acwer.drools.dao.OMdcDao;
import com.acwer.drools.dao.SSBMCBDao;
import com.acwer.drools.dao.SSBMDao;
import com.acwer.drools.dao.Ssbm20Dao;
import com.acwer.drools.dao.UserDao;
import com.acwer.drools.domain.BenchMark;
import com.acwer.drools.domain.CHSDRG;
import com.acwer.drools.domain.ICD10YB;
import com.acwer.drools.domain.ICD9YB;
import com.acwer.drools.domain.JBBM;
import com.acwer.drools.domain.Jbbm20;
import com.acwer.drools.domain.MDC;
import com.acwer.drools.domain.OMDC;
import com.acwer.drools.domain.RpnMsg;
import com.acwer.drools.domain.RqGrp;
import com.acwer.drools.domain.SSBM;
import com.acwer.drools.domain.SSBMCB;
import com.acwer.drools.domain.Ssbm20;
import com.acwer.drools.domain.User;
import com.acwer.utils.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
@Api(tags = "api 控制器")
public class ApiController {

	@Autowired
	private KieSession session;

	@Autowired
	private MDCDao mdcDao;

	@Autowired
	private JBBMDao jbbmDao;

	@Autowired
	private SSBMDao ssbmDao;

	@Autowired
	private SSBMCBDao ssbmcbDao;

	@Autowired
	private MccDao mccDao;

	@Autowired
	private CcDao ccDao;

	@Autowired
	private CHSDRGDao chsDao;

	@Autowired
	private OMdcDao oMdcDao;

	@Autowired
	private BenchMarkDao benchMarkDao;

	@Autowired
	private ICD10YBDao icd10ybDao;

	@Autowired
	private Jbbm20Dao jbbm20Dao;

	@Autowired
	private Ssbm20Dao ssbm20Dao;

	@GetMapping("test2")
	public RpnMsg test2() throws IOException {
		RpnMsg msg = RpnMsg.SUCCESS();
		File file = ResourceUtils.getFile("classpath:db/chs_drg.sql");

		msg.setData(new String(Files.readAllBytes(file.toPath())));

		return msg;
	}

	@Autowired
	private ICD9YBDao icd9ybDao;

	/**
	 * CHS-DRG分组接口
	 */
	/**
	 * <p>
	 * Title: generateGrouping
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @date2021年2月3日
	 * @param json
	 * @return
	 *
	 */
	@PostMapping("generate-grouping")
	public RpnMsg generateGrouping(String json) {
		// 返回数据
		RpnMsg msg = RpnMsg.SUCCESS();
		// 转换json
		RqGrp grp = JSON.parseObject(json, RqGrp.class);
		// JSONArray arr=JSONArray.parse(grp.getODCodes().get(0));
		// grp.setODCodes();
		List<String> getSession = new ArrayList<String>();
		// 定义adrg
		String ADRG = "";
		// 定义最后一位数字
		String forthStr = "5";
		// 最终DRG结果
		String Drg = "";

		/*if (grp.getLenOfStasy() > 60) {
			msg.setMsg("住院天数过大");
			return msg;
		}
		if (grp.getLenOfStasy() < 2) {
			msg.setMsg("住院天数过小");
			return msg;
		}*/

		// 根据主诊断获取MDC(除开先期分组)
		List<String> jbbms = new ArrayList<String>();
		if (org.apache.commons.lang3.StringUtils.isEmpty(grp.getMDCode())) {
			// 1.1 加入主诊断编码
			msg.setMsg("主诊断编码未识别");
			return msg;
			// 1.2 加入其他诊断编码
			/*
			 * if(grp.getODCodes()!=null&&grp.getODCodes().size()>0){
			 * jbbms.addAll(grp.getODCodes()); }
			 */
		} else {
			jbbms.add(getYbCode(grp.getMDCode()));
			// jbbms.add(grp.getMDCode());
		}

		List<JBBM> jbbmLs = new ArrayList<JBBM>();

		List<MDC> mdcs = mdcDao.getMDcList(getYbCode(grp.getMDCode()));
		// List<MDC> mdcs=mdcDao.getMDcList(grp.getMDCode());

		if (grp.getODCodes() != null && grp.getODCodes().size() > 0) {
			List<OMDC> omdcs = oMdcDao.getOMDcList(getYbCodes(grp.getODCodes()), getYbCode(grp.getMDCode()));
			// List<OMDC> omdcs=oMdcDao.getOMDcList(grp.getODCodes(),
			// grp.getMDCode());

			System.out.println(mdcs.stream().map(MDC::getMdc).collect(Collectors.toList()).contains("Z"));
			if (mdcs.stream().map(MDC::getMdc).collect(Collectors.toList()).contains("Z")) {
				System.out.println("进入多发损伤分组流程");
				if (omdcs.size() > 0) {
					System.out.println("确认多发损伤");
					JBBM jbbm = new JBBM();
					jbbm.setJbbm(grp.getMDCode());
					jbbm.setAdrg("ZZ1");
					jbbm.setType(1);
					jbbmLs.add(jbbm);
				} else {
					jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
				}
			} else {
				jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
			}
		} else {
			System.out.println("没有其他诊断");
			jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
		}

		List<JBBM> finallyList = new ArrayList<JBBM>();
		// 2.获取手术编码
		List<SSBM> ssbmLs = new ArrayList<SSBM>();

		List<Ssbm20> ssbm20s = new ArrayList<Ssbm20>();
		// JBBM a= new JBBM();
		// a.getAdrg();

		// final List<SSBM> ssbmcbLsrt=new ArrayList<SSBM>();
		if (grp.getOpCodes() != null && grp.getOpCodes().size() > 0) {
			List<SSBM> ssbmLsrt = new ArrayList<SSBM>();
			ssbm20s = ssbm20Dao.getSsbm20s(grp.getOpCodes());
			ssbmLsrt = ssbmDao.selectByJbbm(getYbOpCodes(grp.getOpCodes()));
			List<SSBMCB> ssbmcbLsrt = ssbmcbDao.selectByJbbm(getYbOpCodes(grp.getOpCodes()));

			ssbmLs.addAll(ssbmLsrt.stream()
					.filter(a -> a.getType().equals(1)
							|| (a.getType().equals(2) && jbbmLs.stream().map(JBBM::getAdrg).collect(Collectors.toList())
									.contains(a.getAdrg()))
							|| (a.getType().equals(3) && ssbmcbLsrt.stream().map(SSBMCB::getAdrg)
									.collect(Collectors.toList()).contains(a.getAdrg())))
					.collect(Collectors.toList()));
			finallyList.addAll(jbbmLs.stream().filter(b ->

			b.getType().equals(1)

					||

					(b.getType().equals(2)
							&& ssbmLs.stream().map(SSBM::getAdrg).collect(Collectors.toList()).contains(b.getAdrg())

					)).collect(Collectors.toList()));
		} else {
			finallyList.addAll(jbbmLs.stream().filter(a -> a.getType().equals(1)).collect(Collectors.toList()));

		}
		/*
		 * if(ssbmLs==null||ssbmLs.size()<=0){ grp.setOpCodes(null); }
		 */

		/*
		 * System.out.println("-----------------处理前--");
		 * System.out.println(finallyList);
		 * 
		 * System.out.println("------------处理前-------"); //
		 * System.out.println(finallyList);
		 * finallyList.remove(Collections.singleton(null));
		 * 
		 * System.out.println("-----------------处理后--");
		 * System.out.println(finallyList);
		 * 
		 * System.out.println("------------处理后-------");
		 */
		if (finallyList.size() <= 0) {
			msg.setMsg("当前主诊断没有找到相关分组");
			return msg;
		}
	/*	System.out.println(finallyList);
		System.out.println(ssbmLs);

		System.out.println(ssbm20s);*/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("jbbms", finallyList);
		map.put("ssbms", ssbmLs);
		map.put("ss20s", ssbm20s);
		session.insert(grp);
		session.insert(map);
		// session.insert(jbbmLs);
		// session.insert(ssbmLs);
		session.startProcess("generate-grouping-flow");
		session.fireAllRules();

		getSession = CollectionUtils.sort((List<String>) session.getGlobal("tmpList"));
		CHSDRG chsdrg = new CHSDRG();
		session.dispose();
		if (getSession == null || getSession.size() <= 0) {
			msg.setMsg("当前诊断没有找到相关分组");
			System.out.println("当前没有该分组");
			Drg = "0000";
			return msg;
		} else {
			ADRG = getSession.get(0);
			/*System.out.println("ADRG分组成功：" + ADRG);
			System.out.println("开始处理第四位");
			System.out.println("查询所有可能的DRG分组");
*/
			/*
			 * if(ADRG.substring(0, 1).equals("M")&&grp.getGender()==2){
			 * msg.setMsg("性别与诊断冲突"); return msg; } if(ADRG.substring(0,
			 * 1).equals("N")&&grp.getGender()==1){ msg.setMsg("性别与诊断冲突");
			 * return msg; } if(ADRG.substring(0,
			 * 1).equals("O")&&grp.getGender()==1){ msg.setMsg("性别与诊断冲突");
			 * return msg; }
			 */
			if (ADRG.substring(0, 1).equals("P") && (grp.getNeoDay() == null || "".equals(grp.getNeoDay()))) {

				msg.setMsg("新生儿出生天数信息缺失，无法判断");

//				System.out.println(msg);
				return msg;
			}
			if (ADRG.substring(0, 1).equals("P") && (grp.getNeoWeight() == null || "".equals(grp.getNeoWeight()))) {
				msg.setMsg("新生儿体重信息缺失，无法判断");
				return msg;
			}

			List<CHSDRG> chsdrgs = chsDao.selectDRGBylst(ADRG);
			if (chsdrgs.size() > 1) {
				System.out.println(chsdrgs);
				if (grp.getODCodes() != null && grp.getODCodes().size() > 0) {
//					System.out.println("其他诊断存在，开始进行下一步分组");
					if (mccDao.selectDRGBylst(getYbCodes(grp.getODCodes()), getYbCode(grp.getMDCode())).size() > 0) {
						// if(mccDao.selectDRGBylst(grp.getODCodes(),
						// grp.getMDCode()).size()>0){
//						System.out.println("MCC存在，最后一位为1");
						chsdrg = CollectionUtils.get4Str(chsdrgs, "1").get(0);
						// msg.setData();
//						System.out.println("分组完毕");
					} else {
//						System.out.println("MCC不存在，判断CC");
						if (ccDao.selectDRGBylst(getYbCodes(grp.getODCodes()), getYbCode(grp.getMDCode())).size() > 0) {
							// if(ccDao.selectDRGBylst(grp.getODCodes(),
							// grp.getMDCode()).size()>0){
//							System.out.println("CC存在，最后一位为3");
							chsdrg = CollectionUtils.get4Str(chsdrgs, "3").get(0);
							// msg.setData();
						} else {
//							System.out.println("CC不存在，最后一位为5");
							chsdrg = CollectionUtils.get4Str(chsdrgs, "5").get(0);
							// msg.setData(CollectionUtils.get4Str(chsdrgs,
							// "5").get(0));
						}
					}
				} else {
//					System.out.println("没有其他分组，最后一位为5");
					// msg.setData(CollectionUtils.get4Str(chsdrgs,
					// "5").get(0));
					chsdrg = CollectionUtils.get4Str(chsdrgs, "5").get(0);
				}
			} else {
				// msg.setData(chsdrgs.get(0));
				chsdrg = chsdrgs.get(0);
//				System.out.println("判断其他诊断是否存在");

			}

		}
		if (!org.apache.commons.lang3.StringUtils.isEmpty(chsdrg.getDRG())) {
			BenchMark benchmark = benchMarkDao.getBenchMark(chsdrg.getDRG());
			chsdrg.setBenchmarkFee(benchmark.getBenchmarkFee());
			chsdrg.setBenchmarkStayDays(benchmark.getBenchmarkStayDays());
			chsdrg.setWeight(benchmark.getWeight());
			chsdrg.setDkkx(benchmark.getDkkx());
		}

		msg.setData(chsdrg);
		// msg.setData(CollectionUtils.sort((List<String>)session.getGlobal("tmpList")));

		return msg;
	}

	/**
	 * 测试规则流
	 * 
	 * @return
	 */
	@PostMapping("/flow-test")
	public RpnMsg test(String json) {
		// 返回数据
		RpnMsg msg = RpnMsg.SUCCESS();
		// 转换json
		RqGrp grp = JSON.parseObject(json, RqGrp.class);
		// JSONArray arr=JSONArray.parse(grp.getODCodes().get(0));
		// grp.setODCodes();
		List<String> getSession = new ArrayList<String>();
		// 定义adrg
		String ADRG = "";
		// 定义最后一位数字
		String forthStr = "5";
		// 最终DRG结果
		String Drg = "";

		// 根据主诊断获取MDC(除开先期分组)
		List<String> jbbms = new ArrayList<String>();
		if (org.apache.commons.lang3.StringUtils.isEmpty(grp.getMDCode())) {
			// 1.1 加入主诊断编码
			msg.setMsg("主诊断编码未识别");
			return msg;
			// 1.2 加入其他诊断编码
			/*
			 * if(grp.getODCodes()!=null&&grp.getODCodes().size()>0){
			 * jbbms.addAll(grp.getODCodes()); }
			 */
		} else {
			jbbms.add(getYbCode(grp.getMDCode()));
			// jbbms.add(grp.getMDCode());
		}

		List<JBBM> jbbmLs = new ArrayList<JBBM>();

		List<MDC> mdcs = mdcDao.getMDcList(getYbCode(grp.getMDCode()));
		// List<MDC> mdcs=mdcDao.getMDcList(grp.getMDCode());

		if (grp.getODCodes() != null && grp.getODCodes().size() > 0) {
			List<OMDC> omdcs = oMdcDao.getOMDcList(getYbCodes(grp.getODCodes()), getYbCode(grp.getMDCode()));
			// List<OMDC> omdcs=oMdcDao.getOMDcList(grp.getODCodes(),
			// grp.getMDCode());

			System.out.println(mdcs.stream().map(MDC::getMdc).collect(Collectors.toList()).contains("Z"));
			if (mdcs.stream().map(MDC::getMdc).collect(Collectors.toList()).contains("Z")) {
				System.out.println("进入多发损伤分组流程");
				if (omdcs.size() > 0) {
					System.out.println("确认多发损伤");
					JBBM jbbm = new JBBM();
					jbbm.setJbbm(grp.getMDCode());
					jbbm.setAdrg("ZZ1");
					jbbm.setType(1);
					jbbmLs.add(jbbm);
				} else {
					jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
				}
			} else {
				jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
			}
		} else {
			System.out.println("没有其他诊断");
			jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
		}

		List<JBBM> finallyList = new ArrayList<JBBM>();
		// 2.获取手术编码
		List<SSBM> ssbmLs = new ArrayList<SSBM>();

		// JBBM a= new JBBM();
		// a.getAdrg();

		// final List<SSBM> ssbmcbLsrt=new ArrayList<SSBM>();
		if (grp.getOpCodes() != null && grp.getOpCodes().size() > 0) {
			List<SSBM> ssbmLsrt = new ArrayList<SSBM>();
			ssbmLsrt = ssbmDao.selectByJbbm(getYbOpCodes(grp.getOpCodes()));
			List<SSBMCB> ssbmcbLsrt = ssbmcbDao.selectByJbbm(getYbOpCodes(grp.getOpCodes()));

			ssbmLs.addAll(ssbmLsrt.stream()
					.filter(a -> a.getType().equals(1)
							|| (a.getType().equals(2) && jbbmLs.stream().map(JBBM::getAdrg).collect(Collectors.toList())
									.contains(a.getAdrg()))
							|| (a.getType().equals(3) && ssbmcbLsrt.stream().map(SSBMCB::getAdrg)
									.collect(Collectors.toList()).contains(a.getAdrg())))
					.collect(Collectors.toList()));
			finallyList.addAll(jbbmLs.stream().filter(b ->

			b.getType().equals(1)

					||

					(b.getType().equals(2)
							&& ssbmLs.stream().map(SSBM::getAdrg).collect(Collectors.toList()).contains(b.getAdrg())

					)).collect(Collectors.toList()));
		} else {
			finallyList.addAll(jbbmLs.stream().filter(a -> a.getType().equals(1)).collect(Collectors.toList()));

		}
		// System.out.println(finallyList);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("jbbms", finallyList);
		map.put("ssbms", ssbmLs);
		session.insert(grp);
		session.insert(map);
		// session.insert(jbbmLs);
		// session.insert(ssbmLs);
		session.startProcess("generate-grouping-flow");
		session.fireAllRules();

		getSession = CollectionUtils.sort((List<String>) session.getGlobal("tmpList"));
		CHSDRG chsdrg = new CHSDRG();
		session.dispose();
		if (getSession == null || getSession.size() <= 0) {
			msg.setMsg("当前诊断没有找到相关分组");
			System.out.println("当前没有该分组");
			Drg = "0000";
			return msg;
		} else {
			ADRG = getSession.get(0);
			System.out.println("ADRG分组成功：" + ADRG);
			System.out.println("开始处理第四位");
			System.out.println("查询所有可能的DRG分组");

			List<CHSDRG> chsdrgs = chsDao.selectDRGBylst(ADRG);
			if (chsdrgs.size() > 1) {
				System.out.println(chsdrgs);
				if (grp.getODCodes() != null && grp.getODCodes().size() > 0) {
					System.out.println("其他诊断存在，开始进行下一步分组");
					if (mccDao.selectDRGBylst(getYbCodes(grp.getODCodes()), getYbCode(grp.getMDCode())).size() > 0) {
						// if(mccDao.selectDRGBylst(grp.getODCodes(),
						// grp.getMDCode()).size()>0){
						System.out.println("MCC存在，最后一位为1");
						chsdrg = CollectionUtils.get4Str(chsdrgs, "1").get(0);
						// msg.setData();
						System.out.println("分组完毕");
					} else {
						System.out.println("MCC不存在，判断CC");
						if (ccDao.selectDRGBylst(getYbCodes(grp.getODCodes()), getYbCode(grp.getMDCode())).size() > 0) {
							// if(ccDao.selectDRGBylst(grp.getODCodes(),
							// grp.getMDCode()).size()>0){
							System.out.println("CC存在，最后一位为3");
							chsdrg = CollectionUtils.get4Str(chsdrgs, "3").get(0);
							// msg.setData();
						} else {
							System.out.println("CC不存在，最后一位为5");
							chsdrg = CollectionUtils.get4Str(chsdrgs, "5").get(0);
							// msg.setData(CollectionUtils.get4Str(chsdrgs,
							// "5").get(0));
						}
					}
				} else {
					System.out.println("没有其他分组，最后一位为5");
					// msg.setData(CollectionUtils.get4Str(chsdrgs,
					// "5").get(0));
					chsdrg = CollectionUtils.get4Str(chsdrgs, "5").get(0);
				}
			} else {
				// msg.setData(chsdrgs.get(0));
				chsdrg = chsdrgs.get(0);
				System.out.println("判断其他诊断是否存在");

			}

		}
		if (!org.apache.commons.lang3.StringUtils.isEmpty(chsdrg.getDRG())) {
			BenchMark benchmark = benchMarkDao.getBenchMark(chsdrg.getDRG());
			chsdrg.setBenchmarkFee(benchmark.getBenchmarkFee());
			chsdrg.setBenchmarkStayDays(benchmark.getBenchmarkStayDays());
			chsdrg.setWeight(benchmark.getWeight());
			chsdrg.setDkkx(benchmark.getDkkx());
		}

		msg.setData(chsdrg);
		// msg.setData(CollectionUtils.sort((List<String>)session.getGlobal("tmpList")));

		return msg;

	}

	@PostMapping("appIndex")
	@ApiOperation("小程序访问接口")
	public RpnMsg appIndex(String json) {
		RpnMsg msg = RpnMsg.SUCCESS();
		System.out.println(json);
		// 转换json
		RqGrp grp = JSON.parseObject(json, RqGrp.class);
		// grp.setMDCode(mDCode);
		// System.out.println(oDCodes);
		/*
		 * grp.setOpCodes(opCodes.); grp.setODCodes(oDCodes);
		 */
		// JSONArray arr=JSONArray.parse(grp.getODCodes().get(0));
		// grp.setODCodes();
		List<String> getSession = new ArrayList<String>();
		// 定义adrg
		String ADRG = "";
		// 定义最后一位数字
		String forthStr = "5";
		// 最终DRG结果
		String Drg = "";

		// 根据主诊断获取MDC(除开先期分组)
		List<String> jbbms = new ArrayList<String>();
		if (org.apache.commons.lang3.StringUtils.isEmpty(grp.getMDCode())) {
			// 1.1 加入主诊断编码
			msg.setMsg("主诊断编码未识别");
			return msg;
		} else {
			jbbms.add(getYbCode(grp.getMDCode()));
			// jbbms.add(grp.getMDCode());
		}

		List<JBBM> jbbmLs = new ArrayList<JBBM>();

		List<MDC> mdcs = mdcDao.getMDcList(getYbCode(grp.getMDCode()));
		// List<MDC> mdcs=mdcDao.getMDcList(grp.getMDCode());

		if (grp.getODCodes() != null && grp.getODCodes().size() > 0) {
			List<OMDC> omdcs = oMdcDao.getOMDcList(getYbCodes(grp.getODCodes()), getYbCode(grp.getMDCode()));
			// List<OMDC> omdcs=oMdcDao.getOMDcList(grp.getODCodes(),
			// grp.getMDCode());

			System.out.println(mdcs.stream().map(MDC::getMdc).collect(Collectors.toList()).contains("Z"));
			if (mdcs.stream().map(MDC::getMdc).collect(Collectors.toList()).contains("Z")) {
				System.out.println("进入多发损伤分组流程");
				if (omdcs.size() > 0) {
					System.out.println("确认多发损伤");
					JBBM jbbm = new JBBM();
					jbbm.setJbbm(grp.getMDCode());
					jbbm.setAdrg("ZZ1");
					jbbm.setType(1);
					jbbmLs.add(jbbm);
				} else {
					jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
				}
			} else {
				jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
			}
		} else {
			System.out.println("没有其他诊断");
			jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
		}

		List<JBBM> finallyList = new ArrayList<JBBM>();
		// 2.获取手术编码
		List<SSBM> ssbmLs = new ArrayList<SSBM>();

		// JBBM a= new JBBM();
		// a.getAdrg();

		// final List<SSBM> ssbmcbLsrt=new ArrayList<SSBM>();
		if (grp.getOpCodes() != null && grp.getOpCodes().size() > 0) {
			List<SSBM> ssbmLsrt = new ArrayList<SSBM>();
			ssbmLsrt = ssbmDao.selectByJbbm(getYbOpCodes(grp.getOpCodes()));
			List<SSBMCB> ssbmcbLsrt = ssbmcbDao.selectByJbbm(getYbOpCodes(grp.getOpCodes()));

			ssbmLs.addAll(ssbmLsrt.stream()
					.filter(a -> a.getType().equals(1)
							|| (a.getType().equals(2) && jbbmLs.stream().map(JBBM::getAdrg).collect(Collectors.toList())
									.contains(a.getAdrg()))
							|| (a.getType().equals(3) && ssbmcbLsrt.stream().map(SSBMCB::getAdrg)
									.collect(Collectors.toList()).contains(a.getAdrg())))
					.collect(Collectors.toList()));
			finallyList.addAll(jbbmLs.stream().filter(b ->

			b.getType().equals(1)

					||

					(b.getType().equals(2)
							&& ssbmLs.stream().map(SSBM::getAdrg).collect(Collectors.toList()).contains(b.getAdrg())

					)).collect(Collectors.toList()));
		} else {
			finallyList.addAll(jbbmLs.stream().filter(a -> a.getType().equals(1)).collect(Collectors.toList()));

		}
		if (ssbmLs == null || ssbmLs.size() <= 0) {
			grp.setOpCodes(null);
		}

		if (finallyList.size() <= 0) {
			msg.setMsg("当前主诊断没有找到相关分组");
			return msg;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("jbbms", finallyList);
		map.put("ssbms", ssbmLs);
		session.insert(grp);
		session.insert(map);
		session.startProcess("generate-grouping-flow");
		session.fireAllRules();

		getSession = CollectionUtils.sort((List<String>) session.getGlobal("tmpList"));
		CHSDRG chsdrg = new CHSDRG();
		session.dispose();
		if (getSession == null || getSession.size() <= 0) {
			msg.setMsg("当前诊断没有找到相关分组");
			System.out.println("当前没有该分组");
			Drg = "0000";
			return msg;
		} else {
			ADRG = getSession.get(0);
			System.out.println("ADRG分组成功：" + ADRG);
			System.out.println("开始处理第四位");
			System.out.println("查询所有可能的DRG分组");

			List<CHSDRG> chsdrgs = chsDao.selectDRGBylst(ADRG);
			if (chsdrgs.size() > 1) {
				System.out.println(chsdrgs);
				if (grp.getODCodes() != null && grp.getODCodes().size() > 0) {
					System.out.println("其他诊断存在，开始进行下一步分组");
					if (mccDao.selectDRGBylst(getYbCodes(grp.getODCodes()), getYbCode(grp.getMDCode())).size() > 0) {
						// if(mccDao.selectDRGBylst(grp.getODCodes(),
						// grp.getMDCode()).size()>0){
						System.out.println("MCC存在，最后一位为1");
						chsdrg = CollectionUtils.get4Str(chsdrgs, "1").get(0);
						// msg.setData();
						System.out.println("分组完毕");
					} else {
						System.out.println("MCC不存在，判断CC");
						if (ccDao.selectDRGBylst(getYbCodes(grp.getODCodes()), getYbCode(grp.getMDCode())).size() > 0) {
							// if(ccDao.selectDRGBylst(grp.getODCodes(),
							// grp.getMDCode()).size()>0){
							System.out.println("CC存在，最后一位为3");
							chsdrg = CollectionUtils.get4Str(chsdrgs, "3").get(0);
							// msg.setData();
						} else {
							System.out.println("CC不存在，最后一位为5");
							chsdrg = CollectionUtils.get4Str(chsdrgs, "5").get(0);
							// msg.setData(CollectionUtils.get4Str(chsdrgs,
							// "5").get(0));
						}
					}
				} else {
					System.out.println("没有其他分组，最后一位为5");
					// msg.setData(CollectionUtils.get4Str(chsdrgs,
					// "5").get(0));
					chsdrg = CollectionUtils.get4Str(chsdrgs, "5").get(0);
				}
			} else {
				// msg.setData(chsdrgs.get(0));
				chsdrg = chsdrgs.get(0);
				System.out.println("判断其他诊断是否存在");

			}

		}
		if (!org.apache.commons.lang3.StringUtils.isEmpty(chsdrg.getDRG())) {
			BenchMark benchmark = benchMarkDao.getBenchMark(chsdrg.getDRG());
			chsdrg.setBenchmarkFee(benchmark.getBenchmarkFee());
			chsdrg.setBenchmarkStayDays(benchmark.getBenchmarkStayDays());
			chsdrg.setWeight(benchmark.getWeight());
			chsdrg.setDkkx(benchmark.getDkkx());
		}

		msg.setData(chsdrg);

		return msg;
	}

	@PostMapping("generate-gntGrpYb")
	public RpnMsg gntGrpYb(String json) {
		System.out.println(json);
		// 返回数据
		RpnMsg msg = RpnMsg.SUCCESS();
		// 转换json
		RqGrp grp = JSON.parseObject(json, RqGrp.class);
		// JSONArray arr=JSONArray.parse(grp.getODCodes().get(0));
		// grp.setODCodes();
		List<String> getSession = new ArrayList<String>();
		// 定义adrg
		String ADRG = "";
		// 定义最后一位数字
		String forthStr = "5";
		// 最终DRG结果
		String Drg = "";

		/*
		 * if (grp.getLenOfStasy() > 60) { msg.setMsg("住院天数过大"); return msg; }
		 * if (grp.getLenOfStasy() < 2) { msg.setMsg("住院天数过小"); return msg; }
		 */

		// 根据主诊断获取MDC(除开先期分组)
		List<String> jbbms = new ArrayList<String>();
		if (org.apache.commons.lang3.StringUtils.isEmpty(grp.getMDCode())) {
			// 1.1 加入主诊断编码
			msg.setMsg("主诊断编码未识别");
			return msg;
			// 1.2 加入其他诊断编码
			/*
			 * if(grp.getODCodes()!=null&&grp.getODCodes().size()>0){
			 * jbbms.addAll(grp.getODCodes()); }
			 */
		} else {
			jbbms.add(grp.getMDCode());
			// jbbms.add(grp.getMDCode());
		}

		List<JBBM> jbbmLs = new ArrayList<JBBM>();

		List<MDC> mdcs = mdcDao.getMDcList(grp.getMDCode());
		// List<MDC> mdcs=mdcDao.getMDcList(grp.getMDCode());

		if (grp.getODCodes() != null && grp.getODCodes().size() > 0) {
			List<OMDC> omdcs = oMdcDao.getOMDcList(grp.getODCodes(), grp.getMDCode());
			if (mdcs.stream().map(MDC::getMdc).collect(Collectors.toList()).contains("Z")) {
				if (omdcs.size() > 0) {
					System.out.println("确认多发损伤");
					JBBM jbbm = new JBBM();
					jbbm.setJbbm(grp.getMDCode());
					jbbm.setAdrg("ZZ1");
					jbbm.setType(1);
					jbbmLs.add(jbbm);
				} else {
					jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
				}
			} else {
				jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
			}
		} else {
			jbbmLs.addAll(jbbmDao.selectJBBMByBM(jbbms));
		}

		List<JBBM> finallyList = new ArrayList<JBBM>();
		// 2.获取手术编码
		List<SSBM> ssbmLs = new ArrayList<SSBM>();

		List<Ssbm20> ssbm20s = new ArrayList<Ssbm20>();
		// JBBM a= new JBBM();
		// a.getAdrg();

		// final List<SSBM> ssbmcbLsrt=new ArrayList<SSBM>();
		if (grp.getOpCodes() != null && grp.getOpCodes().size() > 0) {
			List<SSBM> ssbmLsrt = new ArrayList<SSBM>();
			ssbm20s = ssbm20Dao.getSsbm20s(grp.getOpCodes());
			ssbmLsrt = ssbmDao.selectByJbbm(grp.getOpCodes());
			List<SSBMCB> ssbmcbLsrt = ssbmcbDao.selectByJbbm(grp.getOpCodes());

			ssbmLs.addAll(ssbmLsrt.stream()
					.filter(a -> a.getType().equals(1)
							|| (a.getType().equals(2) && jbbmLs.stream().map(JBBM::getAdrg).collect(Collectors.toList())
									.contains(a.getAdrg()))
							|| (a.getType().equals(3) && ssbmcbLsrt.stream().map(SSBMCB::getAdrg)
									.collect(Collectors.toList()).contains(a.getAdrg())))
					.collect(Collectors.toList()));
			finallyList.addAll(jbbmLs.stream().filter(b ->

			b.getType().equals(1)

					||

					(b.getType().equals(2)
							&& ssbmLs.stream().map(SSBM::getAdrg).collect(Collectors.toList()).contains(b.getAdrg())

					)).collect(Collectors.toList()));
		} else {
			finallyList.addAll(jbbmLs.stream().filter(a -> a.getType().equals(1)).collect(Collectors.toList()));

		}

		if (finallyList.size() <= 0) {
			msg.setMsg("当前主诊断没有找到相关分组");
			return msg;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("jbbms", finallyList);
		map.put("ssbms", ssbmLs);
		map.put("ss20s", ssbm20s);
		session.insert(grp);
		session.insert(map);
		// session.insert(jbbmLs);
		// session.insert(ssbmLs);
		session.startProcess("generate-grouping-flow");
		session.fireAllRules();

		getSession = CollectionUtils.sort((List<String>) session.getGlobal("tmpList"));
		CHSDRG chsdrg = new CHSDRG();
		session.dispose();
		if (getSession == null || getSession.size() <= 0) {
			msg.setMsg("当前诊断没有找到相关分组");
			System.out.println("当前没有该分组");
			Drg = "0000";
			return msg;
		} else {
			ADRG = getSession.get(0);

			if (ADRG.substring(0, 1).equals("M") && grp.getGender() == 2) {
				msg.setMsg("性别与诊断冲突");
				return msg;
			}
			if (ADRG.substring(0, 1).equals("N") && grp.getGender() == 1) {
				msg.setMsg("性别与诊断冲突");
				return msg;
			}
			if (ADRG.substring(0, 1).equals("O") && grp.getGender() == 1) {
				msg.setMsg("性别与诊断冲突");
				return msg;
			}

			if (ADRG.substring(0, 1).equals("P") && (grp.getNeoDay() == null || "".equals(grp.getNeoDay()))) {

				msg.setMsg("新生儿出生天数信息缺失，无法判断");

				System.out.println(msg);
				return msg;
			}
			if (ADRG.substring(0, 1).equals("P") && (grp.getNeoWeight() == null || "".equals(grp.getNeoWeight()))) {
				msg.setMsg("新生儿体重信息缺失，无法判断");
				return msg;
			}

			List<CHSDRG> chsdrgs = chsDao.selectDRGBylst(ADRG);
			if (chsdrgs.size() > 1) {
				System.out.println(chsdrgs);
				if (grp.getODCodes() != null && grp.getODCodes().size() > 0) {
					System.out.println("其他诊断存在，开始进行下一步分组");
					if (mccDao.selectDRGBylst(grp.getODCodes(), grp.getMDCode()).size() > 0) {
						// if(mccDao.selectDRGBylst(grp.getODCodes(),
						// grp.getMDCode()).size()>0){
						System.out.println("MCC存在，最后一位为1");
						chsdrg = CollectionUtils.get4Str(chsdrgs, "1").get(0);
						// msg.setData();
						System.out.println("分组完毕");
					} else {
						System.out.println("MCC不存在，判断CC");
						if (ccDao.selectDRGBylst(grp.getODCodes(), grp.getMDCode()).size() > 0) {
							// if(ccDao.selectDRGBylst(grp.getODCodes(),
							// grp.getMDCode()).size()>0){
							System.out.println("CC存在，最后一位为3");
							chsdrg = CollectionUtils.get4Str(chsdrgs, "3").get(0);
							// msg.setData();
						} else {
							System.out.println("CC不存在，最后一位为5");
							chsdrg = CollectionUtils.get4Str(chsdrgs, "5").get(0);
							// msg.setData(CollectionUtils.get4Str(chsdrgs,
							// "5").get(0));
						}
					}
				} else {
					System.out.println("没有其他分组，最后一位为5");
					// msg.setData(CollectionUtils.get4Str(chsdrgs,
					// "5").get(0));
					chsdrg = CollectionUtils.get4Str(chsdrgs, "5").get(0);
				}
			} else {
				// msg.setData(chsdrgs.get(0));
				chsdrg = chsdrgs.get(0);
				System.out.println("判断其他诊断是否存在");

			}

		}
		if (!org.apache.commons.lang3.StringUtils.isEmpty(chsdrg.getDRG())) {
			BenchMark benchmark = benchMarkDao.getBenchMark(chsdrg.getDRG());
			chsdrg.setBenchmarkFee(benchmark.getBenchmarkFee());
			chsdrg.setBenchmarkStayDays(benchmark.getBenchmarkStayDays());
			chsdrg.setWeight(benchmark.getWeight());
			chsdrg.setDkkx(benchmark.getDkkx());
		}

		msg.setData(chsdrg);
		// msg.setData(CollectionUtils.sort((List<String>)session.getGlobal("tmpList")));

		return msg;
	}

	@ApiOperation("默认展示页")
	@GetMapping("/")
	public RpnMsg index() {

		RpnMsg msg = RpnMsg.SUCCESS();

		msg.setMsg("/swagger-ui.html 查看相关api");
		return msg;
	}

	@Autowired
	UserDao dao;

	/**
	 * 测试h2数据库连接状态
	 * 
	 * @return
	 */
	@GetMapping("h2-test")
	@ApiOperation("测试h2数据库连接状态")
	public RpnMsg h2test() {
		RpnMsg msg = RpnMsg.SUCCESS();

		// QueryWrapper<User> query=new QueryWrapper<User>();

		List<User> users = dao.selectList(null);
		msg.setData(users);
		return msg;
	}

	@GetMapping("getJbbm")
	@ApiOperation("获取疾病编码")
	public RpnMsg getJbbm(String parm) {
		RpnMsg msg = RpnMsg.SUCCESS();

		List<Jbbm20> jbbm20s = jbbm20Dao.getJbbm20(parm);
		msg.setData(jbbm20s);
		return msg;
	}

	@GetMapping("getssbm")
	@ApiOperation("获取疾病编码")
	public RpnMsg getssbm(String parm) {
		RpnMsg msg = RpnMsg.SUCCESS();

		List<Ssbm20> ssbm20s = ssbm20Dao.getJbbm20(parm);
		msg.setData(ssbm20s);
		return msg;
	}

	@GetMapping("mdc-test")
	@ApiOperation("测试MCD分组")
	public RpnMsg MdcTest(String bm) {
		RpnMsg msg = RpnMsg.SUCCESS();
		Map<String, Object> map = new HashMap<>();
		map.put("jbbm", bm);
		List<MDC> mdcs = mdcDao.selectByMap(map);
		msg.setData(mdcs);
		return msg;
	}

	@GetMapping("adrg-test")
	@ApiOperation("测试ADRG分组")
	public RpnMsg ADRGTest(String bm, String ssbm) {
		RpnMsg msg = RpnMsg.SUCCESS();
		if (bm == null || bm.trim().equals("")) {
			msg.setMsg("疾病编码不能为空！！！");
			return msg;
		}
		Map<String, Object> map = new HashMap<>();
		List<String> obj = new ArrayList<String>();
		List<String> obj1 = new ArrayList<String>();

		map.put("jbbm", bm);
		List<JBBM> jbbms = jbbmDao.selectByMap(map);

		List<String> ssbmList = new ArrayList<>();
		if (ssbm != null) {
			String[] ssbmArr = ssbm.split(",");
			if (ssbmArr.length > 0) {
				for (int i = 0; i < ssbmArr.length; i++) {
					ssbmList.add(ssbmArr[i]);
				}

			}
			obj.addAll(jbbms.stream().map(JBBM::getAdrg).collect(Collectors.toList()));
		} else {
			for (int i = 0; i < jbbms.size(); i++) {
				// System.out.println(jbbms.get(i).getAdrg());
				// System.out.println(match("^[A-Z][R-Z][0-9]$",
				// jbbms.get(i).getAdrg()));
				if (jbbms.get(i).getAdrg().matches("^[A-Z][R-Z][0-9]$")) {
					obj.add(jbbms.get(i).getAdrg());
					System.out.println(jbbms.get(i).getAdrg());
				}
			}
		}

		// obj.add(jbbms.stream().map(JBBM::getAdrg).collect(Collectors.toList()));
		if (ssbmList.size() > 0) {
			QueryWrapper<SSBM> ssbmWrapper = new QueryWrapper<>();
			// QueryWrapper<SSBMCB> ssbmCBWrapper=new QueryWrapper<>();
			// ssbmCBWrapper.in("ssbm", ssbmList);
			ssbmWrapper.in("ssbm", ssbmList);
			List<SSBM> ssbms = ssbmDao.selectList(ssbmWrapper);
			// List<SSBMCB> ssbmcbs=ssbmcbDao.selectList(ssbmCBWrapper);
			// obj.add(ssbms);
			// obj.add(ssbmcbs);
			obj1.addAll(ssbms.stream().map(SSBM::getAdrg).collect(Collectors.toList()));

		}

		List<String> adrg = obj.stream().filter(item -> obj1.contains(item)).collect(Collectors.toList());
		if (adrg.size() > 0) {
			msg.setData(adrg);
		} else {
			System.out.println();
			msg.setData(obj.stream().filter(item -> item.matches("^[A-Z][R-Z][0-9]$")));
		}

		msg.setData(adrg.stream().distinct().collect(Collectors.toList()));

		return msg;
	}

	/**
	 * @param regex
	 *            正则表达式字符串
	 * @param str
	 *            要匹配的字符串
	 * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
	 */
	private static boolean match(String regex, String str) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

	/**
	 * 获取医保诊断信息
	 * <p>
	 * Title: getYbCodes
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @date2021年3月2日
	 * @param ls1
	 * @return
	 *
	 */
	private List<String> getYbCodes(List<String> ls1) {
		List<String> codeLst = new ArrayList<String>();
		List<ICD10YB> codes = icd10ybDao.selectJBYbBMByBM(ls1);
		codeLst.addAll(codes.stream().map(ICD10YB::getYbbm).collect(Collectors.toList()));
		for (int i = 0; i < ls1.size(); i++) {
			if (!codes.stream().map(ICD10YB::getJbbm).collect(Collectors.toList()).contains(ls1.get(i))) {
				codeLst.add(ls1.get(i));
			}
		}

		if (codeLst == null || codeLst.size() <= 0) {
			return ls1;
		} else {
			return codeLst;
		}

	}

	private String getYbCode(String code) {
		ICD10YB codes = icd10ybDao.selectJBYbCodeByBM(code);
		if (codes == null) {
			return code;
		} else {
			return codes.getYbbm();
		}

	}

	private List<String> getYbOpCodes(List<String> ls1) {
		List<String> codeLst = new ArrayList<String>();

		List<ICD9YB> codes = icd9ybDao.selectJBYbBMByBM(ls1);
		codeLst.addAll(codes.stream().map(ICD9YB::getYbbm).collect(Collectors.toList()));
		for (int i = 0; i < ls1.size(); i++) {
			if (!codes.stream().map(ICD9YB::getSsbm).collect(Collectors.toList()).contains(ls1.get(i))) {
				codeLst.add(ls1.get(i));
			}
		}

		if (codeLst == null || codeLst.size() <= 0) {
			return ls1;
		} else {
			return codeLst;
		}

	}
}
